<?php

class UserIdentity extends CUserIdentity
{

    private $_id;

    /**
     * Authenticates a user.
     * @return boolean whether authentication succeeds.
     */
    public function authenticate()
    {
	$user = FactoryService::get('user')->getBy('login = :login', array(':login' => $this->username));
	if ($user === null) {		    
	    $this->errorCode = self::ERROR_USERNAME_INVALID;
	    $this->errorMessage = "Нету пользователя с данным логином";	    
	    return false;
	}
	elseif ($user->password !== md5(Yii::app()->params['pwdSalt'] . $this->password)) {
	    $this->errorCode = self::ERROR_PASSWORD_INVALID;
	    $this->errorMessage = "Неверный пароль";
	    return false;
	}
	else {
	    $this->_id = $user->id;	  
	    $this->username = $user->login;	    
	    $this->setState('uploadDir', $user->getUploadPath());	    
	    $this->errorCode = self::ERROR_NONE;
	    $updateData = array(
		'visit_date' => date('Y-m-d H:i'),
		'last_ip' => Yii::app()->getRequest()->getUserHostAddress()
	    );
	    $user->setDbData((object)$updateData);	    
	    FactoryService::get('user')->save($user);
	    return true;
	}	
	return false;
    }
    public function getId()
    {
	return $this->_id;
    }

}